package com.ruoyi.base.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.CollectionUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.ValidationUtils;
import com.ruoyi.system.mapper.SysDeptMapper;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.base.mapper.BaseCourseMapper;
import com.ruoyi.base.domain.BaseCourse;
import com.ruoyi.base.service.IBaseCourseService;

/**
 * 课程Service业务层处理
 * 
 * @author yepanpan
 * @date 2020-11-09
 */
@Service
public class BaseCourseServiceImpl implements IBaseCourseService 
{
    private static final Logger log = LoggerFactory.getLogger(BaseCourseServiceImpl.class);
    
    @Autowired
    private BaseCourseMapper baseCourseMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;

    /**
     * 查询课程
     * 
     * @param id 课程ID
     * @return 课程
     */
    @Override
    public BaseCourse selectBaseCourseById(Long id)
    {
        return baseCourseMapper.selectBaseCourseById(id);
    }

    /**
     * 查询课程列表
     * 
     * @param baseCourse 课程
     * @return 课程
     */
    @Override
    public List<BaseCourse> selectBaseCourseList(BaseCourse baseCourse)
    {
        return baseCourseMapper.selectBaseCourseList(baseCourse);
    }

    /**
     * 新增课程
     * 
     * @param baseCourse 课程
     * @return 结果
     */
    @Override
    public int insertBaseCourse(BaseCourse baseCourse)
    {
        baseCourse.setCreateTime(DateUtils.getNowDate());
        return baseCourseMapper.insertBaseCourse(baseCourse);
    }

    /**
     * 修改课程
     * 
     * @param baseCourse 课程
     * @return 结果
     */
    @Override
    public int updateBaseCourse(BaseCourse baseCourse)
    {
        baseCourse.setUpdateTime(DateUtils.getNowDate());
        return baseCourseMapper.updateBaseCourse(baseCourse);
    }
    
    /**
     * 修改课程状态
     * 
     * @param ids 需要修改的课程ID
     * @param status 新的状态
     * @return 结果
     */
    @Override
    public int updateBaseCourseStatus(Long[] ids, String status) {
    	return baseCourseMapper.updateBaseCourseStatus(ids, status);
    }

    /**
     * 批量删除课程
     * 
     * @param ids 需要删除的课程ID
     * @return 结果
     */
    @Override
    public int deleteBaseCourseByIds(Long[] ids)
    {
        return baseCourseMapper.deleteBaseCourseByIds(ids);
    }

    /**
     * 删除课程信息
     * 
     * @param id 课程ID
     * @return 结果
     */
    @Override
    public int deleteBaseCourseById(Long id)
    {
        return baseCourseMapper.deleteBaseCourseById(id);
    }
    
    /**
     * 导入课程数据
     * 
     * @param courseList 课程数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    @Transactional
    public String importCourse(List<BaseCourse> courseList, Boolean isUpdateSupport, String operName) {
    	if (StringUtils.isNull(courseList) || courseList.size() == 0)
        {
            throw new CustomException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (BaseCourse vo : courseList)
        {
            try
            {
                // 验证部门是否存在
            	SysDept dd = sysDeptMapper.selectDeptByName(vo.getDeptName());
            	if(dd == null) {
            		throw new CustomException("部门不"+vo.getDeptName()+"存在");
            	}
            	vo.setDeptId(dd.getDeptId());

            	//验证
            	List<String> vs = ValidationUtils.validate(vo);
            	if(vs != null) {
            		failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、课程 " + vo.getTitle() + " 数据不完整："+ CollectionUtils.join(vs, ","));
                    continue;
            	}
            	
            	//查找旧数据
            	BaseCourse cond = new BaseCourse();
            	cond.setDeptId(dd.getDeptId());
            	cond.setTitle(vo.getTitle());
            	BaseCourse old = baseCourseMapper.findCourse(cond);
                if (StringUtils.isNull(old))
                {
                    this.insertBaseCourse(vo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、课程 " + vo.getTitle() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    vo.setId(old.getId());
                    this.updateBaseCourse(vo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、课程 " + vo.getTitle() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、课程 " + vo.getTitle() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、课程 " + vo.getTitle() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
